package javanlp;

import java.io.*;
import opennlp.tools.chunker.ChunkSample;
import opennlp.tools.chunker.ChunkerME;
import opennlp.tools.chunker.ChunkerModel;
import opennlp.tools.cmdline.PerformanceMonitor;
import opennlp.tools.cmdline.postag.POSModelLoader;
import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSSample;
import opennlp.tools.postag.POSTaggerME;
import opennlp.tools.tokenize.WhitespaceTokenizer;
import opennlp.tools.util.ObjectStream;
import opennlp.tools.util.PlainTextByLineStream;

public class FeaturesTest {
    private static ChunkerModel getChunkerModel() {
        InputStream modelIn = null;
        ChunkerModel model = null;
        String fld = "C:/Users/lenovolap/Dropbox/java/NLP.models/";
        try {
            modelIn = new FileInputStream(fld+"en-chunker.zip");
            model = new ChunkerModel(modelIn);
        } catch (IOException e) {  
        } finally {
            if (modelIn != null) {
                try {
                    modelIn.close();
                } catch (IOException e) {
                }
            }
        }
        return model;
    }

    public static void main(String[] args) throws IOException {
        String fld = "C:/Users/lenovolap/Dropbox/java/NLP.models/";
        String trainedmodel = fld+"en-pos-maxent.zip";
        POSModel model = new POSModelLoader().load(new File(trainedmodel));
        PerformanceMonitor perfMon = new PerformanceMonitor(System.err, "sent");
        POSTaggerME tagger = new POSTaggerME(model);
        ChunkerModel chunkerModel = getChunkerModel();
        ChunkerME chunker = new ChunkerME(chunkerModel);        
        //String input = "Can anyone help me dig through OpenNLP's 
        //horrible documentation?";
        String input = "The kitchen sink was made in Japan and Korea."
                + "\nSpeedy Michael Jordan ran quickly to Brooklyn.";
        ObjectStream<String> lineStream =
                new PlainTextByLineStream(new StringReader(input));
        
        Spans x = new Spans(0, 2);
        Spans r = new Spans(3, 5);
        Spans y = new Spans(6, 6);

        // start the performance monitor
        perfMon.start();
        String line;
        // tokenize the sentence and tag them
        while ((line = lineStream.read()) != null) {
         String wsTokenizerLine[] = WhitespaceTokenizer.INSTANCE.tokenize(line);
         String[] tags = tagger.tag(wsTokenizerLine);
         String chunkedTags[] = chunker.chunk(wsTokenizerLine, tags);
          
         for (String chunk : chunkedTags) {
                System.out.println(chunk);
            }

            // sentence with POS tags
            POSSample sample = new POSSample(wsTokenizerLine, tags);
            System.out.println(sample.toString());
            
            // sentence with POS tags and NP chunking
            ChunkSample chunkSample = 
                    new ChunkSample(wsTokenizerLine, tags, chunkedTags);
            System.out.println(chunkSample.nicePrint());
            
            boolean feats[] = FeatureExtractor
                    .getFeatures(wsTokenizerLine, tags, x, r, y);
            for ( int i = 0; i < 20; i++) {
                System.out.print(i);
                System.out.print("\t");
                System.out.println(feats[i]);
            }            
            perfMon.incrementCounter();
        }
        // print out the perofrmance result
        perfMon.stopAndPrintFinalResult();
    }
}